module(..., package.seeall)
-- 模块功能：紧急报警逻辑
-- @author zhangfenghe
-- @module access_control_system.emergencyAlarmLogic
-- @license MIT
-- @copyright openLuat
-- @release 2024.02.27
require "pins"
require "mqtt"
require "misc"
require "nvm"
require "config"
require "mqttHostOutMsg"
require "utilsACS"
require "displayLogic"
require "doorLocStatFeedback"
-- 初始化配置文件
nvm.init("config.lua", true)
-- 获取配置文件信息
local slaveNo, hostNo = nvm.get("ClientNo"), nvm.get("HostNo")
local topic, sendMsg = "", ""
local function gpio28IntFnc(msg)
    log.info("testGpioSingle.gpio28IntFnc", msg, GetGpio28Fnc())
    if msg == cpu.INT_GPIO_NEGEDGE then
        log.info(
            ">>>>>>>>>>>下降沿出发紧急预警，通过网络发给主机，主机会有声音和门号提示！！<<<<<<<<<<<<<<<<<")
        -- 开门
        if doorLocStatFeedback.OpenDoorSendMsgTime ~= nil then
            doorLocStatFeedback.OpenDoorSendMsgTime = sys.timerStop(
                                                          doorLocStatFeedback.OpenDoorSendMsgTime)
        end
        utilsACS.EmergencyAlarmLogic()
        if doorLocStatFeedback.OpenDoorDisplay then
            displayLogic.OpenDisplay()
        end
        -- 向主机发送紧急报警
        topic = "acs/" .. hostNo
        sendMsg = "{\"action\":\"alarmOpen \",\"slaveNo\":\"" .. slaveNo ..
                      "\"}"
        mqttHostOutMsg.InsertHostMsg(topic, sendMsg, 0)
    elseif msg == cpu.INT_GPIO_POSEDGE then
        log.info(
            ">>>>>>>>>>>上升沿紧急预警关闭，通过网络发给主机，主机会有报警音关闭和门号提示！！<<<<<<<<<<<<<<<<<")
        -- 关门
        utilsACS.EmergencyAlarmCancel()
        topic = "acs/" .. hostNo
        sendMsg = "{\"action\":\"alarmClos \",\"slaveNo\":\"" .. slaveNo ..
                      "\"}"
    end
end

-- 下降沿中断时：msg为cpu.INT_GPIO_POSEDGE；下降沿中断时：msg为cpu.INT_GPIO_NEGEDGE
GetGpio28Fnc = pins.setup(pio.P0_28, gpio28IntFnc)
